"""Sample protoserv server in python."""

__author__ = 'gatis@google.com (Igor Gatis)'

import calc
import protoserv.server as server

class CalcServiceImpl(calc.CalcService.BlockingInterface):
  def Calculate(rpc, req, resp):
    if req.operation == calc.CalculateRequest.ADD:
      resp.value = req.x + req.y
    elif req.operation == calc.CalculateRequest.SUB:
      resp.value = req.x - req.y
    elif req.operation == calc.CalculateRequest.MUL:
      resp.value = req.x * req.y
    elif req.operation == calc.CalculateRequest.DIV:
      if req.y == 0:
        raise Exception('y cannot be zero')
      resp.value = req.x / req.y
    else:
      raise server.RemoteException('Unknown operation: %d' %
                                   req.operation)

def main():
  rpc_man = server.RpcManager()
  rpc_man.add_endpoint('http', 'http://*:*/rpc')
  rpc_man.register_service(CalcService.new_skel(CalcServiceImpl())
  rpc_man.serve()

if __name__ == '__main__':
  main()
